<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\meal;

use app\common\model\meal\OrderGoods as OrderGoodsModel;

/**
 * 订单商品模型
 * Class OrderGoods
 * @package app\store\model
 */
class OrderGoods extends OrderGoodsModel
{

    public function getListAll(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getQueryFilter($param);
        // 设置订单类型条件
        $dataTypeFilter = $this->getFilterDataType($param['dataType']);
        // 获取数据列表
        return $this
            ->alias('order')
            ->field('order.*,o.buyer_remark,address.name,address.phone,address.bed_no,address.build_no,address.room,o.order_no')
            ->leftJoin('foods_order o', 'o.order_id = order.order_id')
            ->leftJoin('foods_order_address address', 'address.order_id = order.order_id')
            ->where($dataTypeFilter)
            ->where($filter)
            ->where('o.pay_status', 20)
            ->where('o.is_delete', '=', 0)
            ->order(['order.create_time' => 'desc'])
            ->select()->toArray();
    }
    /**
     * 订单列表
     * @param array $param
     * @return mixed
     */
    public function getList(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getQueryFilter($param);
        // 设置订单类型条件
        $dataTypeFilter = $this->getFilterDataType($param['dataType']);
        // 获取数据列表
        $data['list'] = $this
            ->alias('order')
            ->field('order.*,o.buyer_remark,address.name,address.phone,o.order_no')
            ->leftJoin('foods_order o', 'o.order_id = order.order_id')
            ->leftJoin('foods_order_address address', 'address.order_id = order.order_id')
            ->where($dataTypeFilter)
            ->where($filter)
            ->where('o.pay_status', 20)
            ->where('o.is_delete', '=', 0)
            ->order(['order.create_time' => 'desc'])
            ->paginate(10);

        $data['totalPrice'] = $this
            ->alias('order')
            ->field('order.*,address.name,address.phone,o.order_no')
            ->leftJoin('foods_order o', 'o.order_id = order.order_id')
            ->leftJoin('foods_order_address address', 'address.order_id = order.order_id')
            ->where($dataTypeFilter)
            ->where($filter)
            ->where('o.pay_status', 20)
            ->where('o.is_delete', '=', 0)
            ->sum('order.total_pay_price');

        return $data;
    }
/**
 * 设置检索查询条件
 * @param array $param
 * @return array
 */
    private function getQueryFilter(array $param): array
    {
        // 默认参数
        $params = $this->setQueryDefaultValue($param, [
            'searchType'    => '', // 关键词类型 (10订单号 20会员昵称 30会员ID 40收货人姓名 50收货人电话)
            'goodsName'     => '', // 关键词内容
            'orderSource'   => -1, // 订单来源
            'payMethod'     => '', // 支付方式
            'deliveryType'  => -1, // 配送方式
            'extractShopId' => 0,  // 自提门店ID
            'betweenTime'   => [], // 起止时间
            'logids'        => [], // 会员ID
        ]);
        // 检索查询条件
        $filter = [];
        // 关键词
        if (! empty($params['goodsName'])) {
            $filter[] = ['address.name|address.phone|o.order_no', 'like', '%' . $params['goodsName'] . '%'];
        }
        // 起止时间
        if (! empty($params['betweenTime'])) {
            $times    = between_time($params['betweenTime']);
            $filter[] = ['order.date', '>=', Date('Y-m-d', $times['start_time'])];
            $filter[] = ['order.date', '<=', Date('Y-m-d', $times['end_time'])];
        }
        // 订单来源
        $params['orderSource'] > -1 && $filter[] = ['order_source', '=', (int) $params['orderSource']];
        // 支付方式
        ! empty($params['payMethod']) && $filter[] = ['pay_method', '=', $params['payMethod']];
        // 配送方式
        $params['deliveryType'] > -1 && $filter[] = ['delivery_type', '=', (int) $params['deliveryType']];
        // 自提门店id
        $params['extractShopId'] > 0 && $filter[] = ['extract_shop_id', '=', (int) $params['extractShopId']];
        if (! empty($params['logids'])) {

            $filter[] = ['order.order_goods_id', 'in', $params['logids']];
        }
        return $filter;
    }
    /**
     * 设置订单类型条件
     * @param string $dataType
     * @return array
     */
    private function getFilterDataType(string $dataType): array
    {
        // 数据类型
        $filter = [];
        switch ($dataType) {
            case 'all':
                break;
            case 'breakfast':
                $filter[] = ['type', '=', 1];

                break;
            case 'lunch':
                $filter[] = ['type', '=', 2];

                break;
            case 'dinner':
                $filter[] = ['type', '=', 3];

                break;

        }
        return $filter;
    }

}
